package com.bnz.gateway.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
/**
 * 功能：自定义过滤器
 */
@Component
@Slf4j
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {

	public LogGatewayFilterFactory(){
		super(LogGatewayFilterFactory.Config.class);
	}

	@Override
	public List<String> shortcutFieldOrder() {
		return Arrays.asList("consoleLog","cacheLog");
	}

	@Override
	public GatewayFilter apply(Config config) {
		return (exchange, chain) -> {
			if(config.isCacheLog()){
				log.info("使用了缓冲区日志功能...");
			}
			if(config.isConsoleLog()){
				log.info("使用了控制台日志功能...");
			}
			return chain.filter(exchange);
		};
	}
	@Data
	static class Config{
		private boolean consoleLog;     //控制台打印日志
		private boolean cacheLog;       //缓冲区打印日志
	}
}